package com.codegeek.project.slope.service.impl;

import com.codegeek.common.exception.CustomException;
import com.codegeek.common.utils.StringUtils;
import com.codegeek.project.slope.domain.SlopeSensor;
import com.codegeek.project.slope.mapper.SlopeSensorMapper;
import com.codegeek.project.slope.service.ISlopeSensorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 传感器Service业务层处理
 *
 * @author codegeek
 * @date 2020-02-20
 */
@Service
public class SlopeSensorServiceImpl implements ISlopeSensorService {
    @Autowired
    private SlopeSensorMapper slopeSensorMapper;

    /**
     * 查询传感器
     *
     * @param sensorId 传感器ID
     * @return 传感器
     */
    @Override
    public SlopeSensor selectSlopeSensorById(Long sensorId) {
        return slopeSensorMapper.selectSlopeSensorById(sensorId);
    }

    /**
     * 查询传感器列表
     *
     * @param slopeSensor 传感器
     * @return 传感器
     */
    @Override
    public List<SlopeSensor> selectSlopeSensorList(SlopeSensor slopeSensor) {
        return slopeSensorMapper.selectSlopeSensorList(slopeSensor);
    }

    /**
     * 查询传感器列表(设备管理)
     *
     * @param slopeSensor 传感器
     * @return 传感器
     */
    @Override
    public List<SlopeSensor> selectSlopeSensorList2(SlopeSensor slopeSensor) {
        return slopeSensorMapper.selectSlopeSensorList2(slopeSensor);
    }

    /**
     * 新增传感器
     *
     * @param slopeSensor 传感器
     * @return 结果
     */
    @Override
    public int insertSlopeSensor(SlopeSensor slopeSensor) {
        return slopeSensorMapper.insertSlopeSensor(slopeSensor);
    }

    /**
     * 修改传感器
     *
     * @param slopeSensor 传感器
     * @return 结果
     */
    @Override
    public int updateSlopeSensor(SlopeSensor slopeSensor) {
        return slopeSensorMapper.updateSlopeSensor(slopeSensor);
    }

    /**
     * 批量删除传感器
     *
     * @param sensorIds 需要删除的传感器ID
     * @return 结果
     */
    @Override
    public int deleteSlopeSensorByIds(Long[] sensorIds) {
        return slopeSensorMapper.deleteSlopeSensorByIds(sensorIds);
    }

    /**
     * 删除传感器信息
     *
     * @param sensorId 传感器ID
     * @return 结果
     */
    @Override
    public int deleteSlopeSensorById(Long sensorId) {
        return slopeSensorMapper.deleteSlopeSensorById(sensorId);
    }

    /**
     * 修改状态
     *
     * @param slopeSensor
     * @return
     */
    @Override
    public int changeStatus(SlopeSensor slopeSensor) {
        return slopeSensorMapper.changeStatus(slopeSensor);
    }

    @Override
    public String importDevice(List<SlopeSensor> slopeSensors, boolean updateSupport, String operName) {
        if (StringUtils.isNull(slopeSensors) || slopeSensors.size() == 0) {
            throw new CustomException("导入设备数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SlopeSensor device : slopeSensors) {
            try {
                // 验证是否存在这个设备
                List<SlopeSensor> flag = selectSlopeSensorList(device);
                if (flag.size() == 0) {
                    this.insertSlopeSensor(device);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、设备 " + device.getDevice().getDeviceName() + " 导入成功");
                } else if (updateSupport) {
                    device.setUpdateBy(operName);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、设备 " + device.getDevice().getDeviceName() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、设备 " + device.getDevice().getDeviceName() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、设备 " + device.getDevice().getDeviceName() + " 导入失败：";
                failureMsg.append(msg);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public SlopeSensor selectSlopeSensorByIdWithoutIsValid(Long id) {
        return slopeSensorMapper.selectSlopeSensorByIdWithoutIsValid(id);
    }

    @Override
    public SlopeSensor selectSlopeSensorByImei(String imei) {
        return slopeSensorMapper.selectSlopeSensorByImei(imei);
    }

    @Override
    public SlopeSensor selectValidSensorByImei(String imei) {
        return slopeSensorMapper.selectValidSensorByImei(imei);
    }

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public List<Map<String, Object>> checkIMEI(String imei) {
        StringBuilder sql = new StringBuilder("SELECT * FROM slope_device WHERE imei = '" + imei + "'");
        return jdbcTemplate.queryForList(sql.toString());
    }
}
